

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;


public class RevisionControlParser_Apache {

	private int revisionID = 0;
	private String logMessage = "";
	private String author = "";
	private String timestamp = "";
	
	public void setRevisionID(int revisionID){
		this.revisionID = revisionID;
	}
	
	public int getRevisionID(){
		return revisionID;
	}
	
	public String getLogMessage(){
		return logMessage;
	}
	
	public String getAuthor(){
		return author;
	}
	
	public String getTimeStamp(){
		return timestamp;
	}
	
	public String downloadPage(int revisionID){
		String content = "";
		try {
			URL url = new URL("http://svn.apache.org/viewvc?view=revision&revision=" + revisionID);
			URLConnection conn = url.openConnection();
			conn.setDoOutput(true);
			InputStream in = null;
			in = url.openStream();
			content = pipe(in,"utf-8",revisionID);		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return content;
	}
	
	public String pipe(InputStream in,String charset, int revisionID) throws IOException {
		StringBuffer s = new StringBuffer();
		
		if(charset==null||"".equals(charset)){
			charset="utf-8";
		}
		
		String rLine = null;
		BufferedReader bReader = new BufferedReader(new InputStreamReader(in,charset));
		PrintWriter pw = null;
		
		FileOutputStream fo = new FileOutputStream("C:\\Research\\MiningSoftwareRepositories\\Traceability-Link-Recovery\\EXPERIMENTS\\VC\\" + revisionID + ".txt");
		OutputStreamWriter writer = new OutputStreamWriter(fo, "utf-8");
		pw = new PrintWriter(writer);
		
		while ( (rLine = bReader.readLine()) != null) {
		
			String tmp_rLine = rLine;
			int str_len = tmp_rLine.length();
		
			if (str_len > 0) {
				s.append(tmp_rLine);
				pw.println(tmp_rLine.replaceAll("\\<.*?>",""));
				//pw.println(tmp_rLine);
				pw.flush();
			}
	
			tmp_rLine = null;
		}
		
		in.close();	
		pw.close();
		return s.toString();
		
	}
	
	public void process(){
		String result = "";
		String content = downloadPage(revisionID);
		String logMessage = "";
		try{
		    String nohtml = content.replaceAll("\\<.*?>","");
		    //System.out.println(nohtml);
            content = nohtml;
            
            int beginIndex = content.indexOf("Log Message:");
            int endIndex = content.lastIndexOf("Changed paths:");
            logMessage = content.substring(beginIndex+12, endIndex).trim();
            //System.out.println(logMessage);
            this.logMessage = logMessage;
            
            beginIndex = content.indexOf("Author:");
            endIndex = content.lastIndexOf("Date:");
            author = content.substring(beginIndex+7, endIndex).trim();
             
            beginIndex = content.indexOf("Date:");
            endIndex = content.lastIndexOf("UTC");
            timestamp = content.substring(beginIndex+5, endIndex).trim();
            
		}catch(Exception e){
			System.out.println();
		}

	}

	
	public static void main(String[] args) {
		RevisionControlParser rcp = new RevisionControlParser();
		for(int i=0; i<)
		rcp.setRevisionID(805321);
		rcp.process();
		String logMessage = rcp.getLogMessage();
		String author = rcp.getAuthor();
		String timestamp = rcp.getTimeStamp();
		System.out.println("Log Message : " + logMessage);
		System.out.println("Auhtor : " + author);
		System.out.println("Timestamp : " + timestamp);
	}

}